﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QList>
#include <QEvent>
#include <QTimer>
#include <QVector>
#include <QThread>
#include <QMainWindow>
#include <QModelIndex>

#include "Help/help.h"
#include "mydatatype.h"
#include "mydashboard.h"
#include "myPlot/mycurveplot.h"
#include "mySerialPort/myserialport.h"
#include "mySerialPort/myserialcombox.h"
#include "myTableWidget/mycomboxdelegate.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    //表格
//    QModelIndex currentItemIndex;
    myComboboxDelegate comBoxDelegate;
    enum CellType {ctName=1000, ctType, ctData, ctIsCurve,ctIsCurve1,ctIsCurve2 };
    enum FieldColNum {colName=0, colType, colData, colIsCurve,colIsCurve1,colIsCurve2 };
    void isEnabledAllFrameBtn(bool checked);
    void creatItemsARow(int curRow, QString name = "dataName", QString type = "char", QString data = "0", QString Curve = " ");
    void isEnableCurvePlotCheckBox(QVector<quint8>& plotXxx, FieldColNum colIsCurvex, bool checked);
    void detectAllCurvePlotCheckBox();

    //定时器
    QTimer* runTimer = nullptr;
    quint16 timingPeriod10ms;
    qlonglong timing10msCounter;
    quint8 parseOrSaveDataTask();
    void refreshTableTask(quint8 xPacket);

    //串口
    bool isOpenSerialPort;
    QThread* serialPortSubThread = nullptr;
    mySerialPort* serialPortModule = nullptr;
    void isEnabledSerialPortPanelBtn(bool checked);
    QVector<quint8>  recvThreadDataBuffer;          //串口线程数据资源同步

    //协议帧
    quint16 frameLen;                       //用来解析整帧数据
    quint8  frameHeaderLen;                 //帧头长度
    quint16 frameDataTypeLen;               //每帧中数据(类型)的个数(不包括帧头帧尾)
    qint16  checkSumPosOnframeDataType;     //没定义校验和 = -1; 有校验和 >= 0
    qint16  TimeStampPosOnframeDataType;    //没定义数据戳 = -1; 有数据戳 >= 0
    QVector<quint8>  frameHeaderNum;        //用来判断帧头长度 和 解析数据
    QVector<quint8>  frameDataType;         //用来判断数据长度 和 解析数据
    QVector<quint8>  frameTailNum;          //一般的仅使用校验和做为帧尾，该链表用处不大
    QVector<qint8>   dataTypeDivisorFactor; //用于3/2bytesToFloat类型的刻度因子
    QVector<QString> frameDataName;         //用于存储和绘图显示

    //解析数据
    myDataType CV;

    //绘图
    bool isOpenCurvePlot;
    bool isStartCurvePlot;
    myCurvePlot* curvePlot = nullptr;
    enum X_AxisType {frameCount=0, TimeStamp};
    X_AxisType XAxis;

    quint8 curveRefreshCycle;
    quint32 clearCurveTiming_3min;
    QVector<quint8>  xIsPlot1,xIsPlot2,xIsPlot3;
    QVector<QString> xIsPlot1Name,xIsPlot2Name,xIsPlot3Name;
    QVector<double> xPlotKeys;                                      //横坐标数值
    QVector<double> xPlot1Value[3],xPlot2Value[3],xPlot3Value[3];   //纵坐标数值
    QVector<quint8> isPlotXyz,isPlotxYz,isPlotxyZ;


    qlonglong xIsPlotPacketCount;           //绘图帧计数
    qlonglong recv_frameCounter;            //接收帧计数

    //仪表盘
    bool isOpenDashBoard;
    qint16 pitchPosOnframeDataType;
    qint16 rollPosOnframeDataType;
    qint16 yawPosOnframeDataType;
    myDashBoard* attDashBoard = nullptr;
    void detectEulerAnglesToDashBoard();

    //文件保存
    QFile txtFile;      //解析后的文本
    QFile txtFileHex;   //未解析的文本
    bool isSaveTxtFile;
    QVector<QString> outStr;
    QVector<quint8>  saveXDataTypeToTxt;

    //帮助
    bool isOpenHelpUi;
    help* helpInfo = nullptr;

signals:
    //串口
    void signal_openSerialPortx(QString portName, qint32 baudRate, qint8 dataBits, qint8 parity, qint8 stopBits);
    void signal_closeSerialPortx();
    //协议帧
    void signal_sendFrameInfoToSerialSubThread(quint16 _frameLen, QVector<quint8> _frameHeaderNum);

private slots:
    //表格
    void slot_clickedItemAction();

    //协议帧
    void on_insertRow_btn_clicked();
    void on_addRow_btn_clicked();
    void on_insertFrameHeader_btn_clicked();
    void on_insertFrameTail_btn_clicked();
    void on_insertTimeStamp_btn_clicked();
    void on_insertCheckSum_btn_clicked();
    void on_deleteRow_btn_clicked();
    void on_clearDataFrame_btn_clicked();
    void on_saveDataFrame_btn_clicked();
    void on_loadDataFrame_btn_clicked();
    void on_confirmDataFrame_btn_clicked(bool checked);

    //串口
    void slot_switchSerialPort(bool checked);
    void slot_showSerialPortxOpenStatus(bool checked);
    void slot_showSerialPortxCloseStauts(bool checked);

    //保存数据
    void on_saveData_btn_clicked();
    //曲线显示
    void on_curveShow_btn_toggled(bool checked);
    //仪表盘显示
    void on_dashboardShow_btn_toggled(bool checked);
    //任务调度
    void slot_taskScheduler();
    //大小端设置
    void on_comboBox_setEndianMode_currentIndexChanged(int index);
    //设置图形界面X轴的物理意义(0:帧计数 1:下位机发的时间戳)
    void on_comboBox_setXAxis_currentIndexChanged(int index);
    //帮助界面
    void on_showHelp_btn_clicked();

protected:
    //重写关闭时间
    void closeEvent(QCloseEvent *event);
};

#endif // MAINWINDOW_H
